﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HPortal.Lib;
using CrystalDecisions.CrystalReports.Engine;
using System.Data;
using System.Web.Security;
using CrystalDecisions.Shared;

namespace HPortal.Backend
{
    public partial class AReport : System.Web.UI.Page
    {
        static ReportDocument red;
        DataTable dtReport = null;
        DataTable dtBonus = null;
        DataSet ds = new DataSet();

        //ParameterFields paramFields;
        //ParameterField paramField, paramField2;
        //ParameterDiscreteValue paramDiscreteValue, paramDiscreteValue2;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                loadReport();
            }

            if (!IsPostBack)
            {
                cboLinhvuc.DataSource = DatabaseHelper.Select("Select LinhvucID,Ten From KH_Linhvuc");
                cboLinhvuc.DataTextField = "Ten";
                cboLinhvuc.DataValueField = "LinhvucID";
                cboLinhvuc.DataBind();
                cboLinhvuc.Items.Insert(0, new ListItem("Tất cả", "0"));
                cboLinhvuc.SelectedIndex = 0;

                cboLoai.DataSource = DatabaseHelper.Select("Select LoaiID,TenLoai From KH_Loai");
                cboLoai.DataTextField = "TenLoai";
                cboLoai.DataValueField = "LoaiID";
                cboLoai.DataBind();
                cboLoai.Items.Insert(0, new ListItem("Tất cả", "0"));
                cboLoai.SelectedIndex = 0;

                cboCap.DataSource = DatabaseHelper.Select("Select CapID,TenCap From KH_Cap");
                cboCap.DataTextField = "TenCap";
                cboCap.DataValueField = "CapID";
                cboCap.DataBind();
                cboCap.Items.Insert(0, new ListItem("Tất cả", "0"));
                cboCap.SelectedIndex = 0;

                cboNam.DataSource = DatabaseHelper.Select("Select Distinct Nam From KH_Tiendo ORDER BY Nam DESC");
                cboNam.DataTextField = "Nam";
                cboNam.DataValueField = "Nam";
                cboNam.DataBind();
                cboNam.Items.Insert(0, new ListItem("Tất cả", "0"));
                cboNam.SelectedIndex = 0;

                cboDonvi.DataSource = DatabaseHelper.Select("Select DonviID,Ten From KH_Donvi ORDER BY Ten DESC");
                cboDonvi.DataTextField = "Ten";
                cboDonvi.DataValueField = "DonviID";
                cboDonvi.DataBind();
                cboDonvi.Items.Insert(0, new ListItem("Tất cả", "0"));
                cboDonvi.SelectedIndex = 0;
            }            
        }

        private Boolean isNVVuKHCNMT()
        {
            String VuKHCNRole = System.Configuration.ConfigurationManager.AppSettings["VuKHCNRole"].ToString();
            if (this.Context.User.Identity.IsAuthenticated)
            {
                string[] arrRoleName = Roles.GetRolesForUser(this.Context.User.Identity.Name);
                foreach (string roleName in arrRoleName)
                    if (roleName == VuKHCNRole)
                        return true;
            }
            return false;
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            red = null;
            loadReport();
            //Response.Write("<script type='text/javascript'> alert('"+cboReport.SelectedValue+"');</script>");
        }
        
        private void loadReport()
        {
            Label1.Text = "";
            if (red == null)
            {
                red = new ReportDocument();
                switch (Int32.Parse(cboReport.SelectedValue))
                {
                    case 1:
                        red.Load(Server.MapPath("../Backend/Report/CR_DMDT_NCKH_PTCN_DE_XUAT.rpt"));                         
                        break;
                    case 2:
                        red.Load(Server.MapPath("../Backend/Report/CR_DMNV_NCDT_VA_NHIEM_VU_DAT_HANG_NAM.rpt"));
                        break;
                    case 3:
                        red.Load(Server.MapPath("../Backend/Report/CR_DMNV_NCKH_VA_PTCN_TIEP_TUC.rpt"));
                        break;
                    default: break;
                }

                //paramFields = new ParameterFields();
                //paramField = new ParameterField();
                //paramDiscreteValue = new ParameterDiscreteValue();
                //paramField.Name = "@Year";
                //paramDiscreteValue.Value = Int32.Parse(cboNam.SelectedValue);
                //paramField.CurrentValues.Add(paramDiscreteValue);
                //paramFields.Add(paramField);

                //paramField2 = new ParameterField();
                //paramDiscreteValue2 = new ParameterDiscreteValue();
                //paramField2.Name = "@Level";
                //paramDiscreteValue2.Value = cboCap.SelectedItem.Text.ToUpper();
                //paramField2.CurrentValues.Add(paramDiscreteValue2);
                //paramFields.Add(paramField2);    

                dtReport = new DataTable();
                dtReport = DatabaseHelper.Select("Exec [KH_TimkiemDetaiTheoNguoidung] " + DatabaseHelper.EscapeQuote(txtKeyword.Text) + "," + cboLinhvuc.SelectedValue + "," + cboLoai.SelectedValue + "," + cboCap.SelectedValue + "," + cboNam.SelectedValue + "," + cboDonvi.SelectedValue + "," + cboTrangthai.SelectedValue + "," + isNVVuKHCNMT() + "," + this.Context.User.Identity.Name);

                dtBonus = new DataTable();
                dtBonus.Columns.Add("Year");
                dtBonus.Columns.Add("Cap");                
                DataRow dr = dtBonus.NewRow();
                dr["Year"] = cboNam.SelectedValue.ToString();
                dr["Cap"] = cboCap.SelectedItem.Text.ToUpper();
                dtBonus.Rows.Add(dr);

                ds.Tables.Clear();
                ds.Tables.Add(dtReport);
                ds.Tables.Add(dtBonus);

                ds.Tables[0].TableName = "DMDT_NCKH";
                ds.Tables[1].TableName = "BONUS";
                red.SetDataSource(ds);

                if (dtReport.Rows.Count == 0)
                {                    
                    red = null;
                    Label1.Text = "Không tìm thấy dữ liệu ! Mời bạn chọn lại tiêu chí tìm kiếm !";
                }
            }
            
            CrystalReportViewer1.ReportSource = red;
            //CrystalReportViewer1.ParameterFieldInfo = paramFields;
            //CrystalReportViewer1.RefreshReport();
        }
    }
}